package com.springapp.mvc.controller;

import com.springapp.mvc.dao.OrderDao;
import com.springapp.mvc.model.ApplyOrder;
import com.springapp.mvc.model.EffectOrder;
import com.springapp.mvc.model.Picture;
import com.springapp.mvc.tools.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * User: yzh
 * Date: 11/6/15
 * Time: 01:48
 */
@Controller
@RequestMapping("/order")
public class ProductController {
    @Autowired
    private OrderDao orderDao;

    @RequestMapping(value = "/addApply.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String getApply(ApplyOrder order) {
        //TODO 添加apply_order
        orderDao.addApply(order);
        return JsonUtil.toJson(order);//json
    }

    @RequestMapping(value = "/getApply.do", method = RequestMethod.GET)
    public String getApply(String page, String type, ModelMap model) {
        //TODO 查询apply_order
        List<ApplyOrder> data = orderDao.getApply(type);
        if (null == data) {
            data = new ArrayList<ApplyOrder>();
        }
        model.addAttribute("data", data);
        model.addAttribute("type", type);
        model.addAttribute("page", page);
        if ("2".equals(page) || "3".equals(page)) {
            return "product";
        }
        return "orderMana";
    }

    @RequestMapping(value = "/getEffect.do", method = RequestMethod.GET)
    public String getEffect(String page, String type, ModelMap model) {
        //TODO 查询apply_order
        List<EffectOrder> data = orderDao.getEffectOrder(type);
        if (null == data) {
            data = new ArrayList<EffectOrder>();
        }
        model.addAttribute("data", data);
        model.addAttribute("type", type);
        model.addAttribute("page", page);

        return "product";
    }

    @RequestMapping(value = "/getApplyDetail.do", method = RequestMethod.GET)
    public String getApplyDetail(String id, ModelMap model) {
        //TODO 查询apply_order
        List<Picture> data = orderDao.getApplyPicture(id);
        model.addAttribute("data", data);
        return "pictureShow";
    }

    @RequestMapping(value = "/editOrder.do", method = RequestMethod.GET)
    public String editOrder(String id, ModelMap model) {
        //TODO 查询apply_order
        ApplyOrder order = orderDao.getApplyById(id);

        int effectId = order.getEffect_order_effect_order();

        if (effectId != 0) {
            EffectOrder effectOrder = orderDao.getEffectOrderById(effectId);
            model.addAttribute("data", effectOrder);
        } else {
            EffectOrder effectOrder = new EffectOrder();
            model.addAttribute("data", effectOrder);
        }
        return "editOrder";
    }

    @RequestMapping(value = "/chgStatus.do", method = RequestMethod.GET)
    public String chgStatus(String page, String id, String status, ModelMap model) {
        //TODO 改变状态

        orderDao.updateStatus(id, status);

//        ApplyOrder applyOrder = orderDao.getApplyById(id);
//
//        orderDao.addToEffect(applyOrder);

        List<ApplyOrder> data = orderDao.getApply("0");
        if (null == data) {
            data = new ArrayList<ApplyOrder>();
        }
        model.addAttribute("data", data);
        model.addAttribute("page", page);

        return "orderMana";//json
    }

    @RequestMapping(value = "/editEffect.do", method = RequestMethod.POST)
    public String editEffect(EffectOrder order) {
        //TODO 修改定单信息

        orderDao.updateEffect(order);

        return "hello";
    }

    @RequestMapping(value = "/publish.do", method = RequestMethod.POST)
    public String publish(String id) {
        //TODO 发布产品
        ApplyOrder applyOrder = orderDao.publish(id);

        return "hello";
    }
}
